package com.gewu.ordlelist.fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.ordlelist.R;
import com.gewu.ordlelist.adapter.thrAdapter;
import com.gewu.ordlelist.base.BaseFragment;
import com.gewu.ordlelist.database.dbbean.ByCar;
import com.gewu.ordlelist.database.dbbean.footers;
import com.gewu.ordlelist.database.dbdao.ByCarDao;
import com.gewu.ordlelist.database.dbdao.footerDao;
import com.gewu.ordlelist.utils.actionUpdate;
import com.gewu.ordlelist.utils.jocUtils;

import java.util.List;

public class thrFragment extends BaseFragment implements actionUpdate {

    private Toolbar toolbar;
    private TextView text;
    private RadioButton action;
    private RadioButton action2;
    private RadioButton action3;
    private RecyclerView rec;
    private TextView time;
    private TextView money;
    private TextView input;

    @Override
    public View FindView(LayoutInflater inflater) {
        return LayoutInflater.from(getContext()).inflate(R.layout.thr_fragment, null, false);
    }

    @Override
    public void initView(View view) {
        toolbar = view.findViewById(R.id.toolbar);
        text = view.findViewById(R.id.text);
        action = view.findViewById(R.id.action);
        action2 = view.findViewById(R.id.action2);
        action3 = view.findViewById(R.id.action3);
        rec = view.findViewById(R.id.rec);
        time = view.findViewById(R.id.time);
        money = view.findViewById(R.id.money);
        input = view.findViewById(R.id.input);
        toolbar.setTitle(setToolbarText());


        input.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (money.getText().toString().equals("总金额:0")) {
                    jocUtils.showDilog(getContext(), "提示", "订单为0,请您点餐后提交订单！").setPositiveButton("ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).show();
                } else {

                    AlertDialog.Builder builder = jocUtils.showDilog(getContext(), "提示", "下单成功").setPositiveButton("ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            action3.setChecked(true);
                            dialog.dismiss();
                        }
                    });

                    AlertDialog.Builder builder1 = jocUtils.showDilog(getContext(), "提示", "正在计算订单金额......").setPositiveButton("ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            action2.setChecked(true);
                            money.setText("正在进行中......");
                            ByCarDao byCarDao = new ByCarDao(getContext());
                            List<ByCar> quer = byCarDao.quer();
                            for (int i = 0; i < quer.size(); i++) {
                                footerDao footerDao = new footerDao(getContext());
                                footers footers = new footers();
                                footers.setMoney(quer.get(i).getMoney());
                                footers.setName(quer.get(i).getName());
                                footers.setNum(quer.get(i).getNum());
                                footerDao.add(footers);
                            }
                            byCarDao.delect();
                            setAdapter();
                            dialog.dismiss();
                        }
                    });

                    builder.show();
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    builder1.show();

                }

            }
        });


    }

    @Override
    public String setToolbarText() {
        return "订单列表";
    }

    @Override
    public void setAdapter() {
        super.setAdapter();
        ByCarDao byCarDao = new ByCarDao(getContext());
        List<ByCar> quer = byCarDao.quer();
        rec.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        thrAdapter thrAdapter = new thrAdapter(quer, this);
        rec.setAdapter(thrAdapter);
        thrAdapter.notifyDataSetChanged();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        setAdapter();
    }

    @Override
    public void shuaxin(int postion) {
        action2.setChecked(false);
        action3.setChecked(false);

        int i1 = 0;
        List<ByCar> quer1 = new ByCarDao(getContext()).quer();

        if (postion>quer1.size()){
            setAdapter();
        }


        for (int i = 0; i < postion; i++) {
            ByCarDao byCarDao = new ByCarDao(getContext());
            List<ByCar> quer = byCarDao.quer();
            for (int q = 0; q < quer.size(); q++) {
                ByCar byCar = quer.get(q);
                i1 += Integer.parseInt(byCar.getMoney()) * (byCar.getNum()-1);
            }
        }
        money.setText("总金额：" + i1 + "元");
    }
}
